<script setup >
import { getBannersAPI,getCategoryAPI,getHotsAPI,getNewGoodsAPI,getLikesAPI } from "@/api/home";
import Navbar from '@/pages/index/components/Navbar.vue';
import Carousel from "@/components/Carousel/Carousel.vue";
import CateScroll from "@/components/CateScroll/CateScroll.vue";
import PoPular from "@/components/PoPular/PoPular.vue";
import GoodS from "@/components/GoodS/GoodS.vue";
import HodBand from "@/components/HodBand/HodBand.vue";
import BeFond from "@/components/BeFond/BeFond.vue";
import {ref } from 'vue'

const LBTList = ref([])// 轮播图
const QTList = ref([]) // 前
const RMTJList = ref([]) // 热门推荐
const XXHWList = ref([]) //新鲜好物
const CNXHList = ref([]) // 猜你喜欢
const page = ref(1) // 猜你喜欢
const pagess = ref(1) // 猜你喜欢

//#region 轮播图
// 轮播图
const getBanners = async () => {
  const { result } = await getBannersAPI()
  LBTList.value = result
};
getBanners()
//#endregion

//#region  前台类目
// 前台类目
const getCategory = async () => {
  const { result } = await getCategoryAPI()
  QTList.value = result
};
getCategory()
//#endregion

//#region 热门推荐
// 热门推荐
const getHots = async () => {
  const { result } = await getHotsAPI()
  RMTJList.value = result
};
getHots()
//#endregion

//#region  新鲜好物
// 新鲜好物
const getNewGoods = async () => {
  const { result } = await getNewGoodsAPI()
  XXHWList.value = result
};
getNewGoods()
//#endregion

//#region  猜你喜欢
// 猜你喜欢
const getLikes= async () => {
  const { result } = await getLikesAPI(page.value)
  const { items,pages } = result
  CNXHList.value.push(...items);
  page.value++;
  pagess.value = pages;
};
getLikes()
//#endregion

//#region 上拉加载
// 上拉加载
const loadMore = () => {
  if (page.value > pagess.value) return
  getLikes()
}
//#endregion

//#region  下拉加载
const refresherTriggered=ref(false)
const refResherrefresh = async() => {
  refresherTriggered.value = true
  XXHWList.value=[]
  RMTJList.value=[]
  QTList.value=[]
  LBTList.value=[]
  CNXHList.value = []
  page.value=1
  await Promise.all([getBanners(), getCategory(), getHots(), getLikes(), getNewGoods()])
  refresherTriggered.value = false
  
}
//#endregion
</script>
<template>
  <view class="content">
    <!-- 顶部导航栏 -->
    <Navbar></Navbar>
    <scroll-view scroll-y class="main" :refresher-triggered="refresherTriggered" refresher-enabled  @refresherrefresh="refResherrefresh" @scrolltolower="loadMore">
      <!-- 轮播图 -->
      <Carousel :LBTList="LBTList" height="280rpx"></Carousel>
      <!-- 前台类目 -->
      <CateScroll :QTList="QTList" height="280rpx"></CateScroll>
      <!-- 热门推荐 -->
      <PoPular :RMTJList="RMTJList"></PoPular>
      <!-- 新鲜好物 -->
      <GoodS :XXHWList="XXHWList"></GoodS>
      <!-- 热门推荐 -->
      <HodBand></HodBand>
      <!-- 猜你喜欢 -->
      <BeFond :CNXHList="CNXHList"></BeFond>
    </scroll-view>
  </view>
</template>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>

